<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class MessageController extends Controller
{
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //发送消息
        $receive_id = session('member_id');
        $num = DB::table('message')->where('readed', 1)->where('receive_mem_id', $receive_id)->count();
        $recnum = DB::table('message')->where('receive_mem_id', $receive_id)->count();
        $sendnum = DB::table('message')->where('send_mem_id', $receive_id)->count();
        $names = DB::table('member')->select('name')->where('permission','>','1')->get();
        return view('/home/myhouse/message/create',['num' => $num, 'recnum' => $recnum, 'sendnum' => $sendnum,'names'=>$names,'success'=>'1']);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
//        dd($request ->all());
        //如果是超级会员查询所有数据
        $receive_id = session('member_id');
        $num = DB::table('message')->where('readed', 1)->where('receive_mem_id', $receive_id)->count();
        $recnum = DB::table('message')->where('receive_mem_id', $receive_id)->count();
        $sendnum = DB::table('message')->where('send_mem_id', $receive_id)->count();
        $data = DB::table('message')->where('readed', 1)->where('receive_mem_id', $receive_id)->where(function ($query) use ($request) {
            $query->where('content', 'like', '%' . $request->input('keywords') . '%');
        })->paginate($request->input('num', 3));
        foreach ($data as $k => $v) {
            //取出发信人用户名
            $send_mem_id = $v->send_mem_id;
            //获取收信人名
            $receive_id = $v->receive_mem_id;
            $arr = DB::table('member')->where('member_id', $send_mem_id)->first();
            $arr1 = DB::table('member')->where('member_id', $receive_id)->first();
            $v->sname = $arr->name;
            $v->rname = $arr1->name;
            //  $v->stime = date('Y-m-d H:i:s',(date()-$v->stime));
            $v->stime = date('Y-m-d H:i:s', $v->stime);
            $v->content = mb_substr(trim($v->content), 0, 20);
        }
        return view('/home/myhouse/message/index', ['data' => $data, 'request' => $request->all(), 'num' => $num, 'recnum' => $recnum, 'sendnum' => $sendnum]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //处理添加
        $data = $request->all();
        $name = $data['name'];
        $content = $data['content'];
        $userarr = DB::table('member')->select('name')->get();
        foreach ($userarr as $v) {
            $names[] = $v->name;
        }
        if (!in_array($name, $names)) {
            return redirect('/home/myhouse/message/create')->with(['success' => '用户名不存在',
                'content' => $content]);
        };
        $arr = DB::table('member')->where('name', $name)->first();
        unset($data['_token']);
        unset($data['name']);
        $data['receive_mem_id'] = $arr->member_id;
        $data['stime'] = time();
        $res = DB::table('message')->insert($data);
        if ($res) {
            return redirect('/home/myhouse/message')->with(['success' => '发送成功']);
        } else {
            return redirect('/home/myhouse/message/create')->with(['success' => '发送失败']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //消息详情
        $data = DB::table('message')->where('receive_mem_id', $id)->get();
        //根据ID获取发件人、收件人name
        $arr = DB::table('member')->where('member_id', $data['0']->send_mem_id)->first();
        $arr1 = DB::table('member')->where('member_id', $data['0']->receive_mem_id)->first();
        //更改为已读
        DB::table('message')->where('receive_mem_id', $id)->where('send_mem_id', $data['0']->send_mem_id)->update(['readed' => 2]);
        $data['0']->sname = $arr->name;
        $data['0']->rname = $arr1->name;
        $data['0']->time = $data['0']->stime;
        $data['0']->stime = date('Y-m-d H:i:s', $data['0']->stime);
        $data = $data['0'];
        $receive_id = session('member_id');
        $num = DB::table('message')->where('readed', 1)->where('receive_mem_id', $receive_id)->count();
        $recnum = DB::table('message')->where('receive_mem_id', $receive_id)->count();
        $sendnum = DB::table('message')->where('send_mem_id', $receive_id)->count();
        return view('/home/myhouse/message/show', ['data' => $data, 'num' => $num, 'recnum' => $recnum, 'sendnum' => $sendnum]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除消息
        $res = DB::table('message')->where('stime', $id)->update(['disabled' => 2]);
        if ($res) {
            return redirect('/home/myhouse/message')->with(['success' => '已移入回收站']);
        } else {
            return redirect('/home/myhouse/message')->with(['success' => '删除失败']);
        }

    }

    //收件箱
    public function receive(Request $request)
    {
        $receive_id = session('member_id');
        $num = DB::table('message')->where('readed', 1)->where('receive_mem_id', $receive_id)->count();
        $recnum = DB::table('message')->where('receive_mem_id', $receive_id)->count();
        $sendnum = DB::table('message')->where('send_mem_id', $receive_id)->count();
        $data = DB::table('message')->where('receive_mem_id', $receive_id)->where(function ($query) use ($request) {
            $query->where('content', 'like', '%' . $request->input('keywords') . '%');
        })->paginate($request->input('num', 3));
        foreach ($data as $k => $v) {

            //取出发信人用户名
            $send_mem_id = $v->send_mem_id;
            //获取收信人名
            $receive_id = $v->receive_mem_id;
            $arr = DB::table('member')->where('member_id', $send_mem_id)->first();
            $arr1 = DB::table('member')->where('member_id', $receive_id)->first();
            $v->sname = $arr->name;
            $v->rname = $arr1->name;
            //  $v->stime = date('Y-m-d H:i:s',(date()-$v->stime));
            $v->stime = date('Y-m-d H:i:s', $v->stime);
            $v->content = mb_substr(trim($v->content), 0, 20);
        }
        return view('/home/myhouse/message/receive', ['data' => $data, 'request' => $request->all(), 'num' => $num, 'recnum' => $recnum, 'sendnum' => $sendnum]);
    }

    //已发送
    public function send(Request $request)
    {
        $send_id = session('member_id');
        $receive_id = session('member_id');
        $num = DB::table('message')->where('readed', 1)->where('receive_mem_id', $receive_id)->count();
        $recnum = DB::table('message')->where('receive_mem_id', $receive_id)->count();
        $sendnum = DB::table('message')->where('send_mem_id', $receive_id)->count();
        $data = DB::table('message')->where('send_mem_id', $send_id)->where(function ($query) use ($request) {
            $query->where('content', 'like', '%' . $request->input('keywords') . '%');
        })->paginate($request->input('num', 3));
        foreach ($data as $k => $v) {
            //取出发信人用户名
            $send_mem_id = $v->send_mem_id;
            //获取收信人名
            $receive_id = $v->receive_mem_id;
            $arr = DB::table('member')->where('member_id', $send_mem_id)->first();
            $arr1 = DB::table('member')->where('member_id', $receive_id)->first();
            $v->sname = $arr->name;
            $v->rname = $arr1->name;
            //  $v->stime = date('Y-m-d H:i:s',(date()-$v->stime));
            $v->stime = date('Y-m-d H:i:s', $v->stime);
            $v->content = mb_substr(trim($v->content), 0, 20);
        }
        return view('/home/myhouse/message/send', ['data' => $data, 'request' => $request->all(), 'num' => $num, 'recnum' => $recnum, 'sendnum' => $sendnum]);
    }


//已删除
    public function delete(Request $request)
    {
        $send_id = session('member_id');
        $receive_id = session('member_id');
        $num = DB::table('message')->where('readed', 1)->where('receive_mem_id', $receive_id)->count();
        $recnum = DB::table('message')->where('receive_mem_id', $receive_id)->count();
        $sendnum = DB::table('message')->where('send_mem_id', $receive_id)->count();
        $data = DB::table('message')->where('receive_mem_id', $send_id)
            ->orwhere('send_mem_id', $send_id)
            ->where('disabled', 2)
            ->where(function ($query) use ($request) {
                $query->where('content', 'like', '%' . $request->input('keywords') . '%');
            })->paginate($request->input('num', 3));
        foreach ($data as $k => $v) {
            //取出发信人用户名
            $send_mem_id = $v->send_mem_id;
            //获取收信人名
            $receive_id = $v->receive_mem_id;
            $arr = DB::table('member')->where('member_id', $send_mem_id)->first();
            $arr1 = DB::table('member')->where('member_id', $receive_id)->first();
            $v->sname = $arr->name;
            $v->rname = $arr1->name;
            //  $v->stime = date('Y-m-d H:i:s',(date()-$v->stime));
            $v->time = $v->stime;
            $v->stime = date('Y-m-d H:i:s', $v->stime);
            $v->content = mb_substr(trim($v->content), 0, 20);
        }
        return view('/home/myhouse/message/delete', ['data' => $data, 'request' => $request->all(),'num' => $num, 'recnum' => $recnum, 'sendnum' => $sendnum]);
    }
        //执行删除
    public function dodelete($id)
    {
        $send_id =session('member_id');
        $res=DB::table('message')
            ->where('receive_mem_id',$send_id)
            ->orwhere('send_mem_id',$send_id)
            ->where('stime',$id)->delete();
        if($res)
        {
            return redirect('/home/myhouse/message') -> with(['success'=>'删除成功']);
        }else{
            return redirect('/home/myhouse/message/delete') -> with(['success'=>'删除失败']);
        }
    }
}